import http from '@/api/http'
import type {
    APIResult,
    APIPaging,
    PaginationParams,
    OutboundConfirmationDTO,
    OutboundCreateParams,
    InboundConfirmationDTO
} from '@/api/types'

export const outboundAPI = {
    // 获取出库单列表
    getList: (
        params: PaginationParams & { status?: string }
    ): Promise<APIResult<APIPaging<OutboundConfirmationDTO>>> => {
        return http.get('/outbound/list', { params })
    },

    // 获取出库单详情
    getDetail: (id: string): Promise<APIResult<OutboundConfirmationDTO>> => {
        return http.get(`/outbound/detail/${id}`)
    },

    // 创建出库单
    create: (data: OutboundCreateParams): Promise<APIResult<string>> => {
        return http.post('/outbound/create', data)
    },

    // 确认出库
    confirm: (id: string): Promise<APIResult<void>> => {
        return http.post(`/outbound/confirm/${id}`)
    },

    // 取消出库单
    cancel: (id: string): Promise<APIResult<void>> => {
        return http.post(`/outbound/cancel/${id}`)
    },

    // 更新出库单
    update: (id: string, data: Partial<OutboundCreateParams>): Promise<APIResult<void>> => {
        return http.patch(`/outbound/update/${id}`, data)
    },

    // 导出出库单
    export: (params: PaginationParams & { status?: string }): Promise<APIResult<Blob>> => {
        return http.get('/outbound/export', { 
            params,
            responseType: 'blob'
        })
    },

    // 批量确认出库单
    batchConfirm: (ids: string[]): Promise<APIResult<void>> => {
        return http.post('/outbound/batch-confirm', { ids })
    },

    // 批量取消出库单
    batchCancel: (ids: string[]): Promise<APIResult<void>> => {
        return http.post('/outbound/batch-cancel', { ids })
    },

    // 获取关联入库单信息
    getRelatedInbound: (inboundId: string): Promise<APIResult<InboundConfirmationDTO>> => {
        return http.get(`/outbound/related-inbound/${inboundId}`)
    },

    // 验证出库库存
    validateStock: (data: {
        materialId: string
        quantity: number
        warehouseId: string
    }): Promise<APIResult<{available: boolean, availableQuantity: number}>> => {
        return http.post('/outbound/validate-stock', data)
    },

    // 获取物料可选仓库
    getMaterialWarehouses: (materialId: string): Promise<APIResult<Array<{
        id: string
        name: string
        currentStock: number
    }>>> => {
        return http.get(`/outbound/material-warehouses/${materialId}`)
    }
}